import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { FreeflowService } from '../freeflow.service';
import { ProcActDef } from '../freeflow.model';
import { SelectItem, DynamicDialogConfig, DynamicDialogRef } from 'primeng/primeng';

@Component({
    templateUrl: './ffact-attr.component.html',
    styleUrls: ['./ffact-attr.component.scss']
})
export class FfActAttrComponent implements OnInit {
    actAttrForm: FormGroup;
    procActDef: ProcActDef = new ProcActDef({});;
    actDefId: number;
    parts: any[];
    selectedParts: any[] = [];
    actDefAttr: any[] = [];
    cols: any[] = [
        { field: 'partId', header: '标识', width: '80px' },
        { field: 'partType', header: '类型', width: '80px' },
        { field: 'partName', header: '名称', width: '' }
    ];
    // 节点类型
    actTypes: SelectItem[] = [];
    // 环节属性
    actProperties: SelectItem[] = [];
    // 环节配置属性
    actAttrs: any[] = []

    constructor(
        private fb: FormBuilder,
        private freeflowService: FreeflowService,
        private ref: DynamicDialogRef,
        private config: DynamicDialogConfig) {
        this.actDefId = this.config.data.actDefId;
    }

    ngOnInit() {
        // 加载字典信息
        this.freeflowService.loadDict().subscribe(data => {
            this.actTypes = data.types;
            this.actProperties = data.propertys;
            this.actAttrs = data.attrs;
            this.freeflowService.loadProcActDef(this.actDefId).subscribe(procActDef => {
                this.procActDef = procActDef;
                let actDefAttrId = [];
                for (let i = 0; i < this.actAttrs.length; i++) {
                    let value = Math.pow(2, this.actAttrs[i].actAttrId);
                    if ((this.procActDef.actDefAttrId & value) == value) {
                        actDefAttrId.push(this.actAttrs[i].actAttrId + "");
                    }
                }
                this.actDefAttr = actDefAttrId
                this.buildForm();
            });
            this.freeflowService.listProcDefActParts(this.actDefId).subscribe(parts => {
                this.parts = parts;
                this.parts.forEach(ele => {
                    if (ele['relationId'] > 1) {
                        this.selectedParts.push(ele);
                    }
                });
            });
        });
        this.buildForm();
    }

    save() {
        let actDef = this.actAttrForm.value;
        let actDefAttrId = 0;
        if (this.actDefAttr) {
            for (let i = 0; i < this.actDefAttr.length; i++) {
                actDefAttrId += Math.pow(2, this.actDefAttr[i]);
            }
        }
        actDef['actDefAttrId'] = actDefAttrId;
        // 节点属性
        if (!actDef.actPropertyId) actDef.actPropertyId = 1;
        this.actProperties.forEach(ele => {
            if (ele['actPropertyId'] == actDef.actPropertyId) {
                actDef['actPropertyName'] = ele['actPropertyName']
            }
        });
        // 节点类型
        if (!actDef.actTypeId) actDef.actTypeId = 1;
        this.actTypes.forEach(ele => {
            if (ele['actTypeId'] == actDef.actTypeId) {
                actDef['actTypeName'] = ele['actTypeName']
            }
        });
        this.freeflowService.updateProcActDef(actDef).subscribe(actDef => {
            let partIds = '';
            this.selectedParts.forEach(e => {
                partIds += e['partId'] + ","
            })
            this.freeflowService.saveUpdateActPart(this.actDefId, partIds).subscribe(data => {
                this.ref.close(actDef);
            });
        })
    }

    cancel() {
        this.ref.close();
    }


    buildForm(): void {
        this.actAttrForm = this.fb.group({
            'actDefId': [this.procActDef.actDefId],
            'actDefName': [this.procActDef.actDefName],
            'warnTime': [this.procActDef.warnTime],
            'limitTime': [this.procActDef.limitTime],
            'actTypeId': [this.procActDef.actTypeId],
            'actPropertyId': [this.procActDef.actPropertyId]
        });
    }

}
